package com.boot.main.service.impl;

import com.boot.main.utils.IsNotEmptyUtil;
import com.boot.main.entity.CcomRole;
import com.boot.main.entity.CcomRoleMenu;
import com.boot.main.entity.CcomRolePermission;
import com.boot.main.mapper.CcomRoleMapper;
import com.boot.main.mapper.CcomRoleMenuMapper;
import com.boot.main.mapper.CcomRolePermissionMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class RoleService implements IRoleService {
    @Resource
    private CcomRoleMapper roleMapper;

    @Resource
    private CcomRoleMenuMapper roleMenuMapper;

    @Resource
    private CcomRolePermissionMapper rolePermissionMapper;

    @Override
    public Set<String> getPermissionByUserName(String username){
        List<String> permissions = roleMapper.getPermissionByUserName(username);
        return new HashSet<String>(permissions);
    }

    @Override
    public ResponseData getRoles(Integer page, Integer limit, String name){
        List<CcomRole> items = roleMapper.getRoles((page-1)*limit, limit, name);
        Integer count = roleMapper.getTotalCount();
        Map map = new HashMap();
        map.put("datas", items);
        map.put("count", count);
        if(!items.isEmpty()){
            for (CcomRole item:items){

            }
            return ResponseDataUtil.success( "查询成功", map);
        }
        return ResponseDataUtil.success("暂无角色信息！", map);
    }

    @Override
    public ResponseData saveRole(CcomRole item){
        int item_num = roleMapper.saveRole(item);
        long item_id = item.getId();
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("新增角色成功！", null);
        } else {
            return ResponseDataUtil.error("新增角色失败！", null);
        }
    }

    @Override
    public ResponseData updateRole(CcomRole item){
        int item_num = roleMapper.updateRole(item);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success( "修改角色成功！", null);
        } else {
            return ResponseDataUtil.error( "修改角色失败！", null);
        }
    }

    @Override
    public ResponseData deleteRole(Long id){
        int item_num = roleMapper.deleteRole(id);
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("删除角色成功！", null);
        } else {
            return ResponseDataUtil.error( "删除角色失败！", null);
        }
    }

    @Override
    public ResponseData getAllRoles(){
        List<CcomRole> items = roleMapper.getAllRoles();
        if(items.isEmpty()){
            return ResponseDataUtil.success("暂无角色信息！", items);
        } else {
            return ResponseDataUtil.success("查询成功！", items);
        }
    }

    @Override
    public ResponseData saveRoleMenu(CcomRoleMenu roleMenu){
        String menuIds = roleMenuMapper.getMenuByRoleId(roleMenu.getRole_id());
        int item_num = -1;
        if(menuIds != null){
            item_num = roleMenuMapper.updateRoleMenu(roleMenu);
        }else{
            item_num = roleMenuMapper.saveRoleMenu(roleMenu);
        }
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("保存成功", null);
        }else {
            return ResponseDataUtil.error("保存失败", null);
        }
    }

    @Override
    public ResponseData saveRolePermission(CcomRolePermission rolePermission){
        String permissionIds = rolePermissionMapper.getPermissionByRoleId(rolePermission.getRole_id());
        int item_num = -1;
        if(permissionIds != null){
            item_num = rolePermissionMapper.updateRolePermission(rolePermission);
        }else{
            item_num = rolePermissionMapper.saveRolePermission(rolePermission);
        }
        if(!IsNotEmptyUtil.isEmpty(item_num)) {
            return ResponseDataUtil.success("保存成功", null);
        }else {
            return ResponseDataUtil.error("保存失败", null);
        }
    }
}
